from system.object import Object
from system.gameServer import gameServer
from system.eventServer import eventServer

import random

class Ball(Object):
    def __init__(self, parent, type = None, name = None):
        Object.__init__(self,parent,type,name)
        
        size = random.uniform(10,50)
        color = (random.random(),random.random(),random.random())
        
        box = self.addChild("box")
        box.setVar("renderOp2d","rect")
        box.setVar("color",color)
        
        self.setVar("renderOp2d","circle")
        self.setVar("color",color)
        self.setVar("size",size)
        self.setVar("scale",(size,size))
        self.setVar("pos",(random.uniform(size,gameServer.screenw-size),random.uniform(size,gameServer.screenh-size)))
        self.setVar("vel",(random.uniform(-50,50),random.uniform(-50,50)))
        self.setVar("rvel",random.uniform(-3,3))
        self.setVar("rot",(0,0,random.uniform(0,7)))
        self.setVar("static",True)
        
        eventServer.register("update",self.update)
        
    def update(self, cmd, args):
        dtime = args['dtime']
        
        vx, vy = self.getVar("vel")
        px, py = self.getVar("pos")
        lx = px
        ly = py
        
        px += vx * dtime
        py += vy * dtime
        
        size = self.getVar("size")
        
        if px < size or px > gameServer.screenw - size:
            px = lx
            vx *= -1
            
        if py < size or py > gameServer.screenh - size:
            py = ly
            vy *= -1
            
        rot = self.getVar("rot")
        self.setVar("rot",(rot[0],rot[1],rot[2] + self.getVar("rvel") * dtime))
            
        self.setVar("vel",(vx,vy))
        self.setVar("pos",(px,py))
